import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-02-09
 * Time: 15:00
 */
class Solution10 {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals,(a, b)->(a[0] - b[0]));//左端点升序排序
        List<int[]> list = new ArrayList<>();
        for(int[] x : intervals){
            int m = list.size();
            if(m > 0 && list.get(m-1)[1] >= x[0]){//可以合并区间
                list.get(m-1)[1] = Math.max(list.get(m-1)[1],x[1]);
            }else{
                list.add(x);
            }
        }
        int[][] result = new int[list.size()][];
        for(int i = 0 ; i < list.size() ; i++){
            result[i] = list.get(i);
        }
        return result;
    }
}
public class Test10 {
}
